﻿using System;
using System.Collections.Generic;
using System.Text;

namespace daSyntaxAnalyser.SyntaxTree
{
    using daBase.Objects;

    public class ProgramElement : FileElement
    {
        private List<IdElement> parameters;
        private List<ISyntaxTreeElement> implementations;
        private CompoundStatementElement compound;

        public List<IdElement> Params { get { return parameters; } set { parameters = value; } }
        public List<ISyntaxTreeElement> Implementations { get { return implementations; } set { implementations = value; } }
        public CompoundStatementElement Compound { get { return compound; } set { compound = value; } }

        public ProgramElement(TokenInfo StartInfo, string Path, IdElement Name, List<IdElement> Params, UsesElement Uses, List<ISyntaxTreeElement> Implementations, CompoundStatementElement Compound, List<TokenInfo> Comments)
            : base(StartInfo, Path, Name, Uses, Comments)
        {
            parameters = Params;
            implementations = Implementations;
            compound = Compound;
        }

        public override string ToString()
        {
            string result = string.Format("program {0}", Name);
            if (parameters != null)
                result += string.Format("({0})", parameters.Join(", "));
            result += ";\r\n\r\n";
            if (uses != null)
                result += string.Format("{0}\r\n\r\n", uses);
            if ((implementations != null) && (implementations.Count > 0))
                result += string.Format("{0}\r\n\r\n", implementations.Join("\r\n"));
            return string.Format("{0}{1}", result, (compound != null) ? string.Format("{0}.", compound) : "end.");
        }

        protected override IEnumerator<ISyntaxTreeElement> CreateEnumerator()
        {
            IEnumerator<ISyntaxTreeElement> baseEnumerator = base.CreateEnumerator();
            while (baseEnumerator.MoveNext())
                yield return baseEnumerator.Current;
            if (parameters != null)
                foreach (IdElement parameter in parameters)
                    yield return parameter;
            if (implementations != null)
                foreach (ISyntaxTreeElement implementation in implementations)
                    yield return implementation;
            if (compound != null)
                yield return compound;
        }
    }
}
